package com.ygqh.baby.controller.mobile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.aliyun.oss.OSSClient;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ygqh.baby.ao.FileType;
import com.ygqh.baby.ao.FocusType;
import com.ygqh.baby.ao.PageKey;
import com.ygqh.baby.model.YgFocusModel;
import com.ygqh.baby.po.YgFileTemplate;
import com.ygqh.baby.service.FreemarkerService;
import com.ygqh.baby.service.YgFileTemplateService;
import com.ygqh.baby.service.YgFocusService;

@Controller
@RequestMapping("/mobile/test")
public class TestController {

	@Autowired
	private YgFocusService ygFocusService;
	@Autowired
	private YgFileTemplateService ygFileTemplateService;

	@RequestMapping(value = "/testJSONP", method = RequestMethod.GET)
	@ResponseBody
	public JSONPObject testJSONP(HttpServletRequest request, HttpServletResponse response, @RequestParam String callback)
			throws Exception {
		List l = new ArrayList();
		for (int i = 0; i < 2; i++) {
			Map m = new HashMap();
			m.put("name", "测试");
			l.add(m);
		}

		return new JSONPObject(callback, l );
	}
	
	@RequestMapping(value = "/testFileUpload", method = RequestMethod.GET)
	@ResponseBody
	public String testFileUpload()
			throws Exception {
		
		// endpoint以杭州为例，其它region请按实际情况填写
		String endpoint = "http://oss-cn-shanghai.aliyuncs.com";
		// accessKey请登录https://ak-console.aliyun.com/#/查看
		String accessKeyId = "0dNk59qQYl55Senm";
		String accessKeySecret = "JhD2MOdBTVDLxzVzFsrmA3LbzR17mt";
		// 创建OSSClient实例
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		// 上传文件
		ossClient.putObject("yiigoo-tmp", "woshihonglingjin.docx", new File("E:/shiro.docx"));
		// 关闭client
		ossClient.shutdown();
		
		return "success";
	}
	@Autowired
	private FreemarkerService freemarkerService;
	@RequestMapping(value = "/crateHTML", method = RequestMethod.GET)
	@ResponseBody
	public String crateHTML() throws IOException {

		WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
		ServletContext servletContext = webApplicationContext.getServletContext();
		Map result = new HashMap();
		List<YgFocusModel> focusModel = ygFocusService.searchModel( FocusType.Focus, PageKey.Index.name());
        List<YgFileTemplate> imglist = ygFileTemplateService.find(FileType.Mall);
		Map<String,List<YgFileTemplate>> imglistMap =  imglist.stream().collect(Collectors.groupingBy(YgFileTemplate::getAreaName));
        result.put("focusModel",focusModel);
		result.put("imglist",imglistMap);
		freemarkerService.crateHTML("index.ftl","index1.html",result);
		return "ok";
	}
	public static void main(String[] args) {
		List<String> lst = new ArrayList<String>();
		lst.add("123");
		lst.add("1234");
		lst.add("12345");
		lst.add("123456");
		boolean string = lst.stream().filter(i -> i.equals("1")).findFirst().isPresent();
		System.out.println(string);
	}
}
